import { storage_keys } from '@/constants/storage';
import Taro from '@tarojs/taro';
import { action,computed,observable } from 'mobx';
import { StoreMeta } from 'types/core.store';
import { StoreModules,Store } from './core.store';
import { routers } from '../router';
export interface InterfaceGitlabToken {
    access_token: string
    token_type: string;
    expires_in: number;
    refresh_token: string;
    created_at: number
}
export interface InterfaceGitlabUser {
    id: number;
    username: string;
    name: string;
    avatar_url: string;
    web_url: string;
}
export interface InterfaceUser {
    email: string;
    expiresTime: string;
    gitlab_open_id: string;
    lastLoginTime: string;
    name: string;
    phone: string;
    role: string;
    token: string;
    uuid: string;
    _id: string;
    roleNo: number;
}
@StoreModules
export default class UserStore extends Store {
    static meta: StoreMeta = {
        ...Store.meta,
    }
    @observable private _gitlab_token = {
        access_token: '',
        token_type: '',
        expires_in: 0,
        refresh_token: '',
        created_at: 0,
    }
    @observable private _gitlab_user = {
        id: 0,
        username: '',
        name: '',
        avatar_url: '',
        web_url: '',
    }
    @observable private _user = {
        email: '',
        expiresTime: '',
        gitlab_open_id: '',
        lastLoginTime: '',
        name: '',
        phone: '',
        role: '',
        roleNo: 0,
        token: '',
        uuid: '',
        _id:'',
    }
    @computed get gitlab_token() {
        const gitlab_token = Taro.getStorageSync(storage_keys.gitlab_token) as InterfaceGitlabToken
        if (this._gitlab_token.access_token) {
            return this._gitlab_token;
        }
        else if (gitlab_token && gitlab_token.access_token) {
            return gitlab_token;
        }
        else {
            return this._gitlab_token
        }
    }
    @computed get gitlab_user() {
        const gitlab_user = Taro.getStorageSync(storage_keys.gitlab_user) as InterfaceGitlabUser
        if (this._gitlab_user.username) {
            return this._gitlab_user;
        }
        else if (gitlab_user && gitlab_user.username) {
            return gitlab_user;
        }
        else {
            return this._gitlab_user
        }
    }
    @computed get user() {
        const user = Taro.getStorageSync(storage_keys.admin_user) as InterfaceUser
        if (this._user.token&&this._user.uuid) {
            return this._user;
        }
        else if (user && user.token&&user.uuid) {
            return user;
        }
        else {
            return this._user
        }
    }
    @computed get isLogin() {
        if (this.gitlab_token.access_token && this.user.token) {
            return true;
        }
        return false
    }
    /** 鉴权，未登录跳转至登录页 */
    authentication() {
        if (!this.isLogin) {
            Taro.navigateTo({
                url:routers.login
            })
        }
    }
    @action setOauthToken(token: InterfaceGitlabToken) {
        this._gitlab_token = token;
    }
    @action setGitlabUser(user:InterfaceGitlabUser) {
        this._gitlab_user = user;
    }
    @action setUser(user:InterfaceUser) {
        this._user = user;
    }
}
